import {defineStore} from "pinia";
import http from "../utils/http"
 export let useKnowledge = defineStore("knwledge",{
    state:()=>{
        return{
            knowList:[],
            total:0
        }
    },
    actions:{
        //模糊搜索
        async getknow (obj){
            let res = await http.get("/api/knowledge",{params:obj});
            // console.log(res.data.data[0],"res know")
            if(res.status===200){
                //处理后端发过来的数据
                let newarr = res.data.data[0].map(v=>{
                    v.createAt = new Date(v.createAt).toLocaleString()
                    return v
                })
                this.knowList = newarr
                this.total = res.data.data[1] 
            }
        },
        //删除单个
        async removeknow(id){
            let res = await http.delete("/api/knowledge/"+id)
            if(res.status === 200){
                this.getknow({page:1,pageSize:12})
            }
        },
         //改变发布和下线
         async editstatus(obj) {
            let res = await http.patch("/api/knowledge/" + obj.id, { status: obj.status });
            if (res.status == 200) {
                this.getknow({page:1,pageSize:12})
            }
        },
        //更新数据
        async editknow(obj,id){
            // console.log(obj,id)
            let res = await http.patch("/api/knowledge/"+id,obj);
            if(res.status==200){
                this.getknow({page:1,pageSize:12})
            }
        }
    },
    getters:{

    }
})